<template>
  <div class="mainContent" id="mainContent2">
    <el-button
        @click="data.dialogVisible = true"
        class="subscribe-btn"
        type="primary"
        :icon="Bell"
        round>
      订阅成果
    </el-button>
    <el-dialog
        title="订阅成果"
        v-model="data.dialogVisible"
        width="30%"
        height="800px"
    >
      <el-form :model="data" label-width="80px">
        <el-form-item label="邮箱">
          <el-input placeholder="请输入邮箱" v-model="data.email"></el-input>
        </el-form-item>
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="subscribe">订阅</el-button>
      </el-form>
    </el-dialog>
    <!-- 分类和搜索区域 -->
    <el-card class="filter-card" shadow="never">
      <div class="filter-content">
        <!-- 分类区域 -->
        <div class="category-section">
          <div class="category-header">
            <el-icon class="category-icon"><Menu /></el-icon>
            <span class="category-title">成果类型</span>
          </div>
          <div class="category-tags">
            <el-tag
              v-for="(item, index) in data.achievementType"
              :key="index + 'b'"
              class="category-tag"
              :type="page.index === item.id ? 'primary' : ''"
              :effect="page.index === item.id ? 'dark' : 'plain'"
              size="large"
              @click="getTechnology(item.id)"
            >
              {{ item.name }}
              <el-badge
                v-if="item.index > 0"
                :value="item.index"
                class="tag-badge"
              />
            </el-tag>
          </div>
        </div>
        
        <!-- 搜索区域 -->
        <div class="search-section">
          <el-input
            v-model="searchWord"
            class="search-input"
            placeholder="搜索成果名称或关键词"
            :prefix-icon="Search"
            size="large"
            clearable
            @keyup.enter="getKey"
            @clear="handleClearSearch"
          />
          <el-button 
            type="primary" 
            size="large"
            class="search-button"
            @click="getKey"
          >
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
        </div>
      </div>
    </el-card>
    <div class="content">
      <el-skeleton style="width: 100%" :loading="loading" animated>
        <template #template>
          <div style="padding: 14px">
            <el-skeleton-item
                variant="image"
                style="width: 100%; height: 40vh"
            />
          </div>
          <div style="padding: 14px">
            <el-skeleton :rows="5" animated style="width: 100%"/>
          </div>
        </template>
        <template #default>
          <div class="contentRes">
            <achievementCard
                class="resCard"
                :achievement="item"
                v-for="(item, index) in data.achievementByPage"
                :key="index + 'r'"
            ></achievementCard>
          </div>
          <div class="bottomLoading">
            <XWLoding v-show="loading2" v-if="!end"></XWLoding>
            <p
                v-else
                align="center"
                style="padding-bottom: 10px; margin-top: 20px"
            >
              <el-divider content-position="center">没有了</el-divider>
            </p>
          </div>
        </template>
      </el-skeleton>
    </div>
  </div>
</template>

<script setup>
import {ref, onMounted, onUpdated, reactive} from 'vue'
import {Search, Bell, Menu} from '@element-plus/icons-vue'
import {Back, TweenMax} from 'gsap'
import axios from '../utils/Axios'
import base from '../api/base'
import achievementCard from '../components/other/achievementCard.vue'
import {ElMessage} from "element-plus";

function subscribe() {

  axios.post("/subscribe", {email: data.email}).then(res => {
    if (res.success) {
      ElMessage({
        showClose: true,
        message: '订阅成功',
        type: 'success',
      })
    } else {
      ElMessage({
        showClose: true,
        message: '订阅失败',
        type: 'error',
      })
    }
  })
  data.dialogVisible = false
  data.email = ''
}

function cancel() {
  data.dialogVisible = false
  data.email = ''
}

const searchWord = ref('')
const loading = ref(true)
const data = reactive({
  dialogVisible: false,
  email: '',
  achievementByPage: [],
  achievementType: [],
})

const page = reactive({
  beg: 1,
  end: 10,
  index: 0,
  keywords: 'null',
})

const loading2 = ref(false)
const count = ref(1)
const end = ref(false)
axios.get(base.getAchievement + '/1/10/0/null').then(res => {
  // console.log(res.achievementByPage)
  data.achievementByPage = res.achievementByPage
  data.achievementType = res.achievementType
  loading.value = false
})

let isMount = false
onMounted(() => {
  isMount = true
})
// let timer
onUpdated(() => {
  // if (timer) {
  //   clearTimeout(timer)
  // }
  // timer = setTimeout(() =>{
  //   if (document.getElementsByClassName('resCard')[0]) {
  //     TweenMax.staggerFrom(".resCard", 1, {opacity: 0, y: 50, ease: Back.easeOut}, 0.1);
  //   }
  // },500)
  if (isMount) {
    TweenMax.staggerFrom(
        '.item',
        1,
        {opacity: 0, x: -50, ease: Back.easeOut},
        0.3
    )
    isMount = false
  }
})

const app = document.getElementById('app')
let isRefresh = false
window.onscroll = function () {
  // 获取距离顶部的距离
  // var scrollTop = app.scrollTop;
  // 获取可视区的高度
  let windowHeight = app.clientHeight
  // 获取滚动条的总高度
  let scrollHeight = app.scrollHeight
  let my =
      document.documentElement.scrollTop ||
      document.body.scrollTop ||
      window.pageYOffset
  // console.log(my+'-'+scrollTop+'-'+windowHeight+'-'+scrollHeight)
  if (my + windowHeight >= scrollHeight - 50 && !end.value) {
    if (!isRefresh && !loading2.value) {
      isRefresh = true
      loading2.value = true
      getArticle().then(res => {
        // console.log(res.data.allArticle.length)
        data.achievementByPage = res.achievementByPage
        loading2.value = false
        isRefresh = false
        if (res.achievementByPage.length < 10 * count.value) {
          end.value = true
        }
      })
    }
  }
}

function getArticle() {
  // page.beg += 8
  page.end += 10
  count.value++
  return axios.get(
      `/achievement/getAchievement/${page.beg}/${page.end}/${page.index}/${page.keywords}`
  )
}

function getKey() {
  loading.value = true
  page.beg = 1
  page.end = 10
  page.keywords = searchWord.value || 'null'
  count.value = 0
  end.value = false
  getArticle().then(res => {
    data.achievementByPage = res.achievementByPage
    if (res.achievementByPage < 10 * count.value) {
      end.value = true
    }
    loading.value = false
  })
  setTimeout(() => {
    if (document.getElementsByClassName('resCard')[0]) {
      TweenMax.staggerFrom(
          '.resCard',
          1,
          {opacity: 0, y: 50, ease: Back.easeOut},
          0.1
      )
    }
  }, 300)
}

function getTechnology(id) {
  loading.value = true
  // 如果点击的是当前已选中的分类，则取消选择
  if (page.index === id) {
    page.index = 0
  } else {
    page.index = id
  }
  page.beg = 1
  page.end = 10
  count.value = 0
  end.value = false
  getArticle().then(res => {
    data.achievementByPage = res.achievementByPage
    if (res.achievementByPage.length < 10 * count.value) {
      end.value = true
    }
    loading.value = false
  })
}

function handleClearSearch() {
  searchWord.value = ''
  getKey()
}
</script>

<style scoped lang="less">
::v-deep .el-divider__text {
  color: #999999;
}

// 修复对话框打开时滚动条消失的问题
::v-deep .el-overlay {
  .el-dialog {
    margin-top: 5vh;
  }
}

// 全局样式，防止对话框打开时body的overflow被设置为hidden
:global(body.el-popup-parent--hidden) {
  overflow: auto !important;
  padding-right: 0 !important;
}

.subscribe-btn {
  position: fixed;
  top: 80px;
  right: 30px;
  z-index: 1000;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  transition: box-shadow 0.3s ease;
  font-weight: 500;
  
  &:hover {
    box-shadow: 0 6px 16px rgba(64, 158, 255, 0.4);
  }
  
  &:active {
    box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
  }
}

@media screen and (max-width: 768px) {
  .subscribe-btn {
    top: 70px;
    right: 15px;
    font-size: 12px;
    padding: 8px 16px;
  }
  
  #mainContent2 {
    width: 100vw;

    .filter-content {
      flex-direction: column;
      gap: 20px;
    }

    .search-section {
      min-width: auto;
      
      .search-button {
        padding: 10px 20px;
        font-size: 14px;
      }
    }
  }
}

.mainContent {
  width: 80vw;
  margin: 80px auto 0;

  .filter-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 16px;
    margin-bottom: 20px;
    overflow: hidden;
    
    ::v-deep .el-card__body {
      padding: 24px;
      background: rgba(255, 255, 255, 0.95);
      backdrop-filter: blur(10px);
    }
  }

  .filter-content {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 32px;
  }

  .category-section {
    flex: 1;
    min-width: 0;
  }

  .category-header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    
    .category-icon {
      font-size: 20px;
      color: #667eea;
      margin-right: 8px;
    }
    
    .category-title {
      font-size: 18px;
      font-weight: 600;
      color: #2c3e50;
      letter-spacing: 1px;
    }
  }

  .category-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    
    .category-tag {
      cursor: pointer;
      transition: all 0.3s ease;
      border-radius: 20px;
      padding: 8px 16px;
      font-weight: 500;
      position: relative;
      
      &:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      }
      
      .tag-badge {
        margin-left: 8px;
      }
    }
  }

  .search-section {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 320px;
    
    .search-input {
      flex: 1;
      
      ::v-deep .el-input__wrapper {
        border-radius: 25px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease;
        
        &:hover {
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
      }
    }
    
    .search-button {
      border-radius: 25px;
      padding: 12px 24px;
      font-weight: 500;
      box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
      transition: all 0.3s ease;
      
      &:hover {
        box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
      }
    }
  }

  .content {
    background-color: white;
    margin: 10px 0 0 0;

    .contentRes {
      //height: 80vh;
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
      justify-items: center;
      grid-row-gap: 20px;
    }
  }
}
</style>
